package com.joeone.controller;

import com.joeone.bean.CommonResult;
import com.joeone.entity.mti.*;
import com.joeone.request.JoeoneMemberRequest;
import com.joeone.request.JoeoneRequest;
import com.joeone.respone.JoeoneDictionaryRespone;
import com.joeone.respone.JoeoneRespone;
import com.joeone.service.*;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@Api(value = "九牧王接口文档", tags = "九牧王接口文档", description = "九牧王接口文档")
@RequestMapping("/joeone")
public class JoeoneController {
    private final PerformanceOverviewService performanceOverviewService;
    private final RetailService retailService;
    private final CategorySalesaAnalysisService categorySalesaAnalysisService;
    private final ChannelAnalysisService channelAnalysisService;
    private final MemberService memberService;

    @ApiOperation(value = "看板查看", notes = "看板查看")
    @PostMapping("/queryPerformanceOverview")
    @ResponseBody
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = JoeoneRespone.class)})
    public CommonResult<JoeoneRespone> queryPerformanceOverview(@RequestBody JoeoneRequest joeoneRequest){
        JoeoneRespone joeoneRespone = new JoeoneRespone();
        PerformanceOverview performanceOverview = new PerformanceOverview();
        if(CollectionUtils.isEmpty(joeoneRequest.getComp())){
            //根据用户id查询出用户所有分公司
            joeoneRequest.setComp(performanceOverviewService.queryFgs(joeoneRequest.getUserId()));
            if(CollectionUtils.isEmpty(joeoneRequest.getComp())){
                return CommonResult.error("用户分公司无数据");
            }
        }
        //业绩总览数据--start
        List<PerformanceOverviewOne> performanceOverviewOneList = performanceOverviewService.queryPerformanceOverviewOne(joeoneRequest);
        List<PerformanceOverviewTwo> performanceOverviewTwoList = performanceOverviewService.queryPerformanceOverviewTwo(joeoneRequest);
        List<PerformanceOverviewThree> performanceOverviewThreeList = performanceOverviewService.queryPerformanceOverviewThree(joeoneRequest);
        performanceOverview.setPerformanceOverviewOneList(performanceOverviewOneList);
        performanceOverview.setPerformanceOverviewTwoList(performanceOverviewTwoList);
        performanceOverview.setPerformanceOverviewThreeList(performanceOverviewThreeList);
        //业绩总览数据--end

        //零售数据--start
        Retail retail = retailService.queryRetail(joeoneRequest);
        performanceOverview.setRetailOneList(retail.getRetailOneList());
        performanceOverview.setRetailTwoList(retail.getRetailTwoList());
        //零售数据--end

        //会员分析数据--start
        List<Member> memberList = memberService.queryMember(joeoneRequest);
        performanceOverview.setMemberList(memberList);
        //会员分析数据--end

        //渠道业态分析数据--start
        ChannelAnalysis channelAnalysis = channelAnalysisService.queryChannelAnalysis(joeoneRequest);
        performanceOverview.setChannelAnalysisOneList(channelAnalysis.getChannelAnalysisOneList());
        performanceOverview.setChannelAnalysisTwoList(channelAnalysis.getChannelAnalysisTwoList());
        //渠道业态分析数据--end

        //大类销售分析数据--start
        Map<String, List<CategorySalesaAnalysis>> map = categorySalesaAnalysisService.queryCategorySalesaAnalysisList(joeoneRequest);
        performanceOverview.setCategorySalesaAnalysisAllList(map.get("all"));//整体
        performanceOverview.setCategorySalesaAnalysisXpList(map.get("xp"));//新品
        performanceOverview.setCategorySalesaAnalysisJpList(map.get("jp"));//旧品
        //大类销售分析数据--end
        joeoneRespone.setPerformanceOverview(performanceOverview);
        return CommonResult.success(joeoneRespone);
    }
    @ApiOperation(value = "查询分公司", notes = "查询分公司")
    @ApiImplicitParam(name = "userId", value = "用户id")
    @PostMapping("/queryComp")
    @ResponseBody
    public CommonResult<List<String>> queryComp(@RequestBody JoeoneMemberRequest joeoneMemberRequest){
        return CommonResult.success(performanceOverviewService.queryFgs(joeoneMemberRequest.getUserId()));
    }
    @ApiOperation(value = "查询字典", notes = "查询字典")
    @PostMapping("/queryDictionary")
    @ResponseBody
    public CommonResult<JoeoneDictionaryRespone> queryDictionary(){
        JoeoneDictionaryRespone joeoneDicRespone = new JoeoneDictionaryRespone();
        List<String> type = performanceOverviewService.queryType();
        List<String> qdyt = performanceOverviewService.queryQDYT();
        joeoneDicRespone.setType(type);
        joeoneDicRespone.setQdyt(qdyt);
        return CommonResult.success(joeoneDicRespone);
    }
    @ApiOperation(value = "查询商品排行", notes = "查询商品排行")
    @PostMapping("/queryGoodsRank")
    @ResponseBody
    public CommonResult<List<GoodsRankVo>> queryGoodsRank(@RequestBody JoeoneRequest joeoneRequest){
        return CommonResult.success(performanceOverviewService.getGoodsRank(joeoneRequest));
    }
}
